package com.lin.community_thumb_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.community_thumb_sys.common.UserConstant;
import com.lin.community_thumb_sys.controller.UserController;
import com.lin.community_thumb_sys.model.domain.Blog;
import com.lin.community_thumb_sys.model.domain.Thumb;
import com.lin.community_thumb_sys.model.domain.User;
import com.lin.community_thumb_sys.model.vo.BlogVO;
import com.lin.community_thumb_sys.service.BlogService;
import com.lin.community_thumb_sys.mapper.BlogMapper;
import com.lin.community_thumb_sys.service.ThumbService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author Lenovo
* @description 针对表【blog】的数据库操作Service实现
* @createDate 2025-08-15 13:52:45
*/
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService{
    @Resource
    private ThumbService thumbService;

    @Override
    public BlogVO getBlogVOById(long blogId, HttpServletRequest request) {
        /**
         * 获取当前会话用户和当前博客 去查询是否有点赞 给到BlogVO
         */
        //当前博客
        Blog blog = this.getById(blogId);
        //当前会话用户
        User loginUser = (User) request.getSession().getAttribute(UserConstant.LOGIN_USER_SESSION_KEY);
        return getBlogVo(loginUser, blog);
    }

    /**
     * 转换博客VO对象
     * @param loginUser
     * @param blog
     * @return
     */
    public BlogVO getBlogVo(User loginUser, Blog blog) {
        BlogVO blogVO = new BlogVO();
        BeanUtils.copyProperties(blog, blogVO);
        //判断当前用户是否点赞该博客
//        LambdaQueryWrapper<Thumb> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Thumb::getUserId, loginUser.getId())
//                .eq(Thumb::getBlogId, blog.getId());
//        Thumb thumb = thumbService.getOne(wrapper);
        Thumb thumb = thumbService.lambdaQuery()
                .eq(Thumb::getUserId, loginUser.getId())
                .eq(Thumb::getBlogId, blog.getId())
                .one();
        blogVO.setHasThumb(thumb != null);
        return blogVO;
    }
}




